package interfaces;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import services.UserServicesProxy;

import com.google.common.base.Strings;

/**
 * @author Savva
 *
 */
public class RegistrationDialog extends JPanel {
	private static final long serialVersionUID = 1L;

	private JTextField loginField = new JTextField(10);
	
	private JPasswordField passwordField = new JPasswordField(10);
	
	private JPasswordField confirmPasswordField = new JPasswordField(10);
	
	private JButton okButton = new JButton("Ок");
	private JButton cancelButton = new JButton("Отмена");
	
	
	private JDialog dialog;
	public RegistrationDialog(JDialog dialog) {
		this.dialog = dialog;
		initComponents();
		initListeners();
	}
	private void initListeners() {
		okButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				String login = loginField.getText();
				if(Strings.isNullOrEmpty(login)) {
					JOptionPane.showMessageDialog(null, "Вы не ввели логин");
					return;
				}
				String string = new String(passwordField.getPassword());
				String confirmString = new String(confirmPasswordField.getPassword());
				if(Strings.isNullOrEmpty(string) || Strings.isNullOrEmpty(confirmString)) {
					JOptionPane.showMessageDialog(null, "Вы не ввели пароль");
					return;
				}
				if(!string.equals(confirmString)) {
					JOptionPane.showMessageDialog(null, "Пароли не совпадают");
					return;
				}
				UserServicesProxy proxy = new UserServicesProxy();
				try {
					int result = proxy.registration(login, string);
					switch (result) {
					case -1:
						JOptionPane.showMessageDialog(null, "Логни введён не верно");
						return;
					case -2:
						JOptionPane.showMessageDialog(null, "Пароль введён не верно. Пароль должен содержать минимум 7 символов");
						return;
					case -3:
						JOptionPane.showMessageDialog(null, "Такой пользователь уже есть в БД");
						return;
					default:
						break;
					}
				} catch (RemoteException e) {
					e.printStackTrace();
				}
				dialog.dispose();
			}
		});
		cancelButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				dialog.dispose();
			}
		});
	}
	private void initComponents() {
		JPanel panel = new JPanel(new GridBagLayout());
		panel.add(new JLabel("Логин"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		panel.add(loginField, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		panel.add(new JLabel("Пароль"), new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		panel.add(passwordField, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		panel.add(new JLabel("Подтвердите пароль"), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		panel.add(confirmPasswordField, new GridBagConstraints(1, 2, 1, 1, 1.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		panel.add(buttonPanel, new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0, 
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
		add(panel);		
	}
	
	public static void showDialog() {
		JDialog dialog = new JDialog();
		RegistrationDialog regDialog = new RegistrationDialog(dialog);
		dialog.add(regDialog);
		dialog.setTitle("Регистрация");
		dialog.setModal(true);
		dialog.setLocationRelativeTo(null);
		dialog.pack();
		dialog.setVisible(true);
	}
}
